package com.vt.auth.handler;

import com.vt.common.security.handler.AuthenticationFailureEvenHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Component;

/**
 * @Auther: PanZha
 * @Date: 2018/10/12 16:26
 */
@Slf4j
@Component
public class DefaultAuthenticationFailureEvenHandler extends AuthenticationFailureEvenHandler {

    /**
     * 处理登录失败方法
     * <p>
     *
     * @param authentication 登录的authentication 对象
     */
    @Override
    public void handle(Authentication authentication, AuthenticationException authenticationException) {
        log.info("用户：{} 登录失败，异常：{}", authentication.getPrincipal(), authenticationException.getLocalizedMessage());

        if (authenticationException instanceof BadCredentialsException)
            throw new BadCredentialsException(authenticationException.getMessage());
    }
}
